<?php
/**
 * Created by PhpStorm.
 * User: zhoper
 * Date: 2018/7/28
 * Time: 15:44
 */
namespace app\controllers;
//header('Access-Control-Allow-Origin:*');

use Yii;
use yii\web\Controller;
use app\models\TestForm;
use app\widgets\Curl;

class TestController extends Controller{
    public $enableCsrfValidation = false;
    public $layout = false;
    public $code = '0';
    public $msg = 'success';
    public $data = '';
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    public function out(){
        $out = [
            'code' => $this->code,
            'msg' => $this->msg,
            'data' => $this->data
        ];
        echo json_encode($out);
    }

    public function actionIndex(){
        $data = file_get_contents('php://input');
        if($data){
            $dataArr = json_decode($data,true);

            foreach ($dataArr as $k => $v){
                $validateArr[] = $k;
            }

            $form = new TestForm();
            $form->addBodyParams($dataArr);  //传值到model
            if ($form->validate()){  //验证数据规则
                $res = Curl::GET('http://www.cheshouye.com/api/weizhang/query_task',$dataArr);
                $this->data = $res;
            }else{
                $this->code = '-10001';
                $this->msg = 'error';
            }
        }else{
            $this->code = '-10001';
            $this->msg = 'data is null!';
        }
        $this->out();  //输出结果json
        exit();
    }
}
?>